﻿using System;
using System.Collections.Generic;
using Service4u2.Common;
using Service4u2.Json;
using MoAds.AdAdapters;

namespace MoAds.AdAdapters.AdMob
{
    /// <summary>
    /// An adapter for showing AdMob ads retrieved via JSON services.
    /// </summary>
    public class AdMobAdapter : AdAdapter
    {
        /// <summary>
        /// The AdMob JSON service.
        /// </summary>
        protected AdMobService adMobService;

        /// <summary>
        /// Gets or sets the ad keywords.
        /// </summary>
        /// <value>The ad keywords.</value>
        public string AdKeywords
        { get; set; }

        /// <summary>
        /// Gets or sets the cookie string.
        /// </summary>
        /// <value>The cookie string.</value>
        public string CookieString
        { get; set; }

        /// <summary>
        /// Gets or sets the publisher id.
        /// </summary>
        /// <value>The publisher id.</value>
        public string PublisherId
        { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [use test mode].
        /// </summary>
        /// <value><c>true</c> if [use test mode]; otherwise, <c>false</c>.</value>
        public bool UseTestMode
        { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="AdMobAdapter"/> class.
        /// </summary>
        public AdMobAdapter()
        {
            adMobService = new AdMobService();
         
            adMobService.GotResult += new EventHandler<EventArgs<AdInfoResponse>>(OnResult);
            adMobService.GotError += new EventHandler<EventArgs<Exception>>(OnException);
        }

        /// <summary>
        /// Parses the keywords.
        /// </summary>
        /// <returns>A list of parsed keywords</returns>
        private List<string> ParseKeywords()
        {
            if (String.IsNullOrEmpty(this.AdKeywords))
                return null;

            if (!this.AdKeywords.Contains(" "))
                return new List<string>() { this.AdKeywords };

            return new List<string>(this.AdKeywords.Split(' '));
        }

        #region AdAdapter Members

        /// <summary>
        /// Handles the GotResult event of the adMobService control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Service4u2.Common.EventArgs&lt;MoAds.AdInfoResponse&gt;"/> instance containing the event data.</param>
        protected virtual void OnResult(object sender, EventArgs<AdInfoResponse> e)
        {
            AdResponse response = new AdResponse()
            {
                AdText = e.Argument.text,
                ClickUrl = e.Argument.url,
                ImageUrl = e.Argument.tile_url ?? e.Argument.banner ?? e.Argument.image_url
            };

            OnResult(response);
        }

        /// <summary>
        /// Gets the ad response asynchronously.
        /// </summary>
        public override void BeginAdRequestAsync()
        {
            var keywords = ParseKeywords();
            adMobService.GetAdInfoAsync(this.PublisherId, this.CookieString, BrowserAgentString, this.UseTestMode, keywords);
        }

        #endregion
    }
}
